/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2020. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */

/*!
 * @file ac780x_rtc.c
 *
 * @brief This file provides rtc integration functions.
 *
 */

/* ===========================================  Includes  =========================================== */
#include "ac780x_rtc_reg.h"

/* ============================================  Define  ============================================ */

/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */
/* RTC callback pointer */
static DeviceCallback_Type s_rtcCallback = (DeviceCallback_Type)NULL;

/* ====================================  Functions declaration  ===================================== */

/* ======================================  Functions define  ======================================== */
/*!
 * @brief Initialize RTC module.
 *
 * @param[in] config: RTC configuration pointer
 * @return none
 */
void RTC_Init(const RTC_ConfigType *config)
{
    DEVICE_ASSERT(NULL != config);

    /* enable rtc system module */
    CKGEN_Enable(CLK_RTC, ENABLE);
    CKGEN_SoftReset(SRST_RTC, ENABLE);

    /* clear int flag */
    RTC_ClearTAF();
    RTC_ClearTOF();
    RTC_ClearPOF();

    /* config RTC clock source */
    RTC_SetClkSource(config->clockSource);

    /* config RTC output */
    RTC_SetRTCOutput(config->clockOutFlag);

    /* config timercnt value */
    (void)RTC_SetTimeCntRegister(config->timeCnt);

    /* config timer prescaler value */
    RTC_SetPrescaler(config->psrValue);

    /* config timer alarm value */
    RTC_SetTimeAlarmRegister(config->alarmValue);

    /* register user callback function */
    s_rtcCallback = config->callBack;

    /* config RTC int */
    RTC_SetTOIE(config->timeCntInterruptEn);

    /* config RTC prescaler int */
    RTC_SetPOIE(config->psrInterruptEn);

    /* config RTC alarm int */
    RTC_SetTAIE(config->alarmInterruptEn);

    /* config rtc nvic interrupt */
    if ((ENABLE == config->timeCntInterruptEn) || (ENABLE == config->psrInterruptEn) || (ENABLE == config->alarmInterruptEn))
    {
        NVIC_EnableIRQ(RTC_IRQn);
    }
    else
    {
        NVIC_DisableIRQ(RTC_IRQn);
        NVIC_ClearPendingIRQ(RTC_IRQn);
    }

    RTC_Enable(ENABLE);
}

/*!
 * @brief RTC De-initialize.
 *
 * @param[in] none
 * @return none
 */
void RTC_DeInit(void)
{
    s_rtcCallback = (DeviceCallback_Type)NULL;
    CKGEN_SoftReset(SRST_RTC, DISABLE);
    CKGEN_Enable(CLK_RTC, DISABLE);
    RTC_Enable(DISABLE);
    NVIC_DisableIRQ(RTC_IRQn);
    NVIC_ClearPendingIRQ(RTC_IRQn);
}

/*!
 * @brief set RTC Callback function.
 *
 * @param[in] callback: call back func
 * @return none
 */
void RTC_SetCallback(DeviceCallback_Type callback)
{
    s_rtcCallback = callback;
}

/*!
 * @brief RTC interrupt request handler.
 *
 * @param[in] none
 * @return none
 */
void RTC_IRQHandler(void)
{
    uint32_t wpara = 0;

    /* store device status */
    wpara = RTC_GetCtrlStatusReg();

    /* clear prescaler flag */
    if (wpara & RTC_SR_POF_Msk)
    {
        RTC_ClearPOF();
    }
    if (wpara & RTC_SR_TOF_Msk)
    {
        RTC_ClearTOF();
    }
    if (wpara & RTC_SR_TAF_Msk)
    {
        RTC_ClearTAF();
    }

    /* excute callback func */
    if (NULL != s_rtcCallback)
    {
        s_rtcCallback(RTC, wpara, 0);
    }
}

/* =============================================  EOF  ============================================== */
